package org.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.domain.MemberCollection;
import org.example.domain.Prod;
import org.example.model.Result;
import org.example.service.MemberCollectionService;
import org.example.utils.AuthUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags="会员收藏商品接口管理")
@RequestMapping("p/collection")
@RestController
public class MemberCollectionController {

    @Autowired
    private MemberCollectionService memberCollectionService;

    @ApiOperation("查询会员收藏商品的数量")
    @GetMapping("/count")
    public Result<Long> loadMemberCollectionProdCount() {
        Long count = memberCollectionService.queryMemberCollectionProdCount();
        return Result.success( count);
    }

    @ApiOperation("分页查询会员收藏商品列表")
    @GetMapping("/prods")
    public Result<Page<Prod>> loadMemberCollectionProdPage(@RequestParam Long current,
                                                           @RequestParam Long size) {
        // 根据会员openid分页查询会员收藏商品列表
        Page<Prod> page = memberCollectionService.queryMemberCollectionProdPageByOpenId(AuthUtils.getMemberOpenId(), current, size);
        return Result.success( page);
    }

    // TODO: 2025/11/05 小程序接口
    @ApiOperation("小程序查询会员收藏商品状态")
    @GetMapping("/isCollection")
    public Result<Boolean> loadMemberIsCollection(@RequestParam Long prodId) {
        long count = memberCollectionService.count( new LambdaQueryWrapper<MemberCollection>()
                .eq(MemberCollection::getOpenId, AuthUtils.getMemberOpenId())
                .eq(MemberCollection::getProdId, prodId));

        return Result.success( 1 == count);
    }

    @ApiOperation("添加或取消收藏商品")
    @PostMapping("/addOrCancel")
    public Result<String> addOrCancelMemberCollection(@RequestBody Long prodId) {
        Boolean flag = memberCollectionService.addOrCancelMemberCollection(AuthUtils.getMemberOpenId(), prodId);
        return Result.handle(flag);
    }

}
